/*River Warren
 *October 15, 2011
 *
 *Inventory.cpp
 *Inventory object to be used by the game Dungeons and Princesses
*/
#include "Inventory.h"


//add item to the inventory
void Inventory::add(Base* item)
{
    Listing listing; //prepare to add to the manifest
    //populate the listing struct
    listing.objectID = item->getObjectID();
    listing.description = item->getDesc();
    listing.objectType = item->getObjectType();
    //list the item
    manifest.push_back(listing);
    //inventory the item
    items.insert(make_pair(item->getObjectID(), item));
    ++numOfItems; //increment number of items
}

//get a key out of inventory
Key* Inventory::getKey(int thisID)
{
    map<int, Base*>::iterator it; //the guy who looks through the warehouse for the item
    it = items.find(thisID); //find the item on the shelf
    if(it == items.end())return NULL;
    Key* thisKey = (Key*)(*it).second; //the packing of the item
    items.erase(it); //take the item off the shelf
    deleteFromManifest(thisID); //remove item from manifest
    return thisKey; //ship the item
}

//get a eqipment type object (sword, helmet etc).
EquipmentType * Inventory::getEquipmentType(int thisID) {
    
    map<int, Base*>::iterator it; //the guy who looks through the warehouse for the item
    it = items.find(thisID); //find the item on the shelf
    if(it == items.end())return NULL;
    EquipmentType* thisEquipment = (EquipmentType*)(*it).second; //the packing of the item
    items.erase(it); //take the item off the shelf
    deleteFromManifest(thisID); //remove item from manifest
    return thisEquipment; //ship the item

    
}

// get a potion type object
Potion * Inventory::getPotion(int thisID) {
    
    map<int, Base*>::iterator it; //the guy who looks through the warehouse for the item
    it = items.find(thisID); //find the item on the shelf
    if(it == items.end())return NULL;
    Potion * thisPotion = (Potion*)(*it).second; //the packing of the item
    items.erase(it); //take the item off the shelf
    deleteFromManifest(thisID); //remove item from manifest
    return thisPotion; //ship the item

}

//delete item from manifest that has been pulled out of inventory
void Inventory::deleteFromManifest(int thisID)
{
    int i;
    for(i = 0; i < numOfItems && manifest[i].objectID != thisID; ++i); //find item on list
    manifest.erase(manifest.begin() + i); //remove item
    --numOfItems; //decrement number of items
}

ostream& operator<<(ostream& out, Inventory& inv)
{
    string typeList[150];
    int i = 0;
    map<int, Base*>::iterator it; //the guy who looks through the warehouse for the item
    it = inv.items.begin();
    typeList[i] = (*it).second->getObjectType();
    out << typeList[i];
    for(++it; it != inv.items.end(); ++it)
    {
        ++i;
        typeList[i] = (*it).second->getObjectType();
        out << ',' << typeList[i];
    }
    cout << endl;
    i = 0;
    for(it = inv.items.begin(); it != inv.items.end(); ++it)
    {
        switch(typeList[i][0])
        {
            case 'E':
                break;
            case 'K':
                out << (*(Key*)(*it).second) << endl;
                break;
            case 'M':
                break;
        }
        ++i;
    }
    return out;
}

istream& operator>>(istream& in, Inventory& inv)
{
    Key k;
    string typeList[150];
    int i = 0;
    char *tk = NULL;// pointers to chars
    char data[MAX] = ""; //to hold line
    in.getline(data,MAX - 1, '\n');
    tk = strtok(data, DELIMS);  //get first token of new line
    while(tk != NULL)
    {
        typeList[i] = tk;
        tk = strtok(NULL, DELIMS); // get next token
        ++i;
    }
    for(int j = 0; j < i; ++j)
    {
        switch(typeList[j][0])
        {
            case 'E':
                break;
            case 'K':
                in >> k;
                inv.add(&k);
                break;
            case 'M':
                break;

        }
    }
    return in;
}
